import {
  baseUrl,
  timeOut
} from '../../utils/base'

// 同一页面请求的次数
let ajaxnum = 0

export function request(config) {
  ajaxnum++
  // 请求前出发Loading效果
  wx.showLoading({
    title: '加载中...',
    mask: true
  })
  return new Promise((resolve, reject) => {
    wx.request({
      url: baseUrl + config.url,
      method: config.method || 'GET',
      data: config.data,
      header: config.header,
      timeOut: config.timeOut || timeOut,
      success: ((res) => {
        resolve(res)
      }),
      fail: ((err) => {
        reject(err)
      }),
      complete: () => {
        ajaxnum--
        if (ajaxnum === 0) {
          // 关闭加载中效果
          wx.hideLoading()
        } 
      }
    })
  })
}